//
//  SPickerTextView.m
//  STool
//
//  Created by Samuel on 15/9/9.
//
//

#import "SPickerTextView.h"

@implementation SPickerTextView
{
    UIPickerView *pickerView;
    UIDatePicker *datePicker;
    UIToolbar *toolbar;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}
- (void)setting:(UIView*)views
{
    self.inputView = views;
    toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(toolbar.frame), 35)];
    UIBarButtonItem *closeBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignFirstResponder)];
    UIBarButtonItem *flexible = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    toolbar.items = @[flexible,closeBtn];
    self.inputAccessoryView = toolbar;
}
#pragma mark - CoustomDelegate
-(void)setCoustomDelegate:(NSObject<SPickerCoustomDelegate> *)coustomDelegate
{
    _coustomDelegate = coustomDelegate;
    if (!pickerView)
        pickerView = [[UIPickerView alloc]init];
        pickerView.dataSource = self;
        pickerView.delegate = self;
        pickerView.showsSelectionIndicator = YES;
    [self setting:pickerView];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    if ([self.coustomDelegate respondsToSelector:@selector(numberWithRowInSPickerTextView)])
        return [self.coustomDelegate numberWithRowInSPickerTextView];
    else
        return 0;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if ([self.coustomDelegate respondsToSelector:@selector(sPickerTextView:stringWithIndexRow:)])
        return [self.coustomDelegate sPickerTextView:self stringWithIndexRow:row];
    else
        return @"";
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if ([self.coustomDelegate respondsToSelector:@selector(sPickerTextView:stringWithIndexRow:)])
        self.text = [self.coustomDelegate sPickerTextView:self stringWithIndexRow:row];
}
#pragma mark - DateDelegate
-(void)setDateDelegate:(NSObject<SPickerDateDelegate> *)dateDelegate
{
    _dateDelegate = dateDelegate;
    if (!datePicker) {
        datePicker = [[UIDatePicker alloc]init];
        [datePicker addTarget:self action:@selector(dateAction:) forControlEvents:UIControlEventValueChanged];
        datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
        
        NSTimeInterval nowTime=[[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSince1970];
        double gets = 365*24*60*60;
        double lastYearTime = nowTime - gets;
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:lastYearTime];
        
        datePicker.minimumDate = confromTimesp;
        datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:0];
    }
    [self setting:datePicker];
}
- (void)dateAction:(UIDatePicker*)sender
{
    NSDateFormatter *dataFormatter = [[NSDateFormatter alloc]init];
    [dataFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    self.text = [dataFormatter stringFromDate:sender.date];
    self.timestamp = [NSString stringWithFormat:@"%.f",[sender.date timeIntervalSince1970]];
}
@end
